package com.example.alipay.pdf;

import cn.org.bjca.seal.esspdf.client.message.ChannelMessage;
import cn.org.bjca.seal.esspdf.client.message.ClientSignBean;
import cn.org.bjca.seal.esspdf.client.tools.AnySignClientTool;
import cn.org.bjca.seal.esspdf.client.utils.ClientUtil;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class PDF {
    static AnySignClientTool anySignClientTool = null;

    static private String resourcesPath;
    static String testPdfFilePath;
    static String testSignedPdfFilePath;
    static String testImageFilePath;
    ResponseEntity responseEntity = null;
    static  String basepath = null;
    static  String resourcepath = null;

    static {
        File directory = new File("../dsvs/src/main/resources");

        try {
            basepath = directory.getCanonicalPath();
            resourcepath =basepath+"\\";
            System.out.println("根本路径："+resourcepath);
        } catch (IOException e) {
            e.printStackTrace();
        }


        String ip = "223.70.139.221";
        int port = 8888;
        try {
            anySignClientTool = new AnySignClientTool(ip, port);
            if (anySignClientTool != null) {
                System.out.println("连接服务成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //连接服务器超时时间，单位是毫秒，默认为5秒，当前为50秒
        anySignClientTool.setTimeout(50000);//连接超时时间设置
        anySignClientTool.setRespTimeout(50000);//响应超时时间

        testPdfFilePath = resourcesPath + "pdf" + File.separator + "test.pdf";
        testSignedPdfFilePath = resourcesPath + "pdf" + File.separator + "signedPDF.pdf";
        testImageFilePath = resourcesPath + "image" + File.separator + "test.gif";

    }
    @Test
    public void ProxySeal() throws Exception {
        String json = resourcepath+"Rpdf\\json.txt";
        String signDataJson = FileUtils.readFileToString(new File( json));
        ChannelMessage message = anySignClientTool.genClientSign(signDataJson);
        System.out.println("状态码："+message.getStatusCode());
        System.out.println("状态信息："+message.getStatusInfo());
        if("200".equals(message.getStatusCode())){//成功
            List<ClientSignBean> signBeanList = message.getClientSignList();
            for (ClientSignBean beanObj : signBeanList) {
                ClientUtil.writeByteArrayToFile(new File(resourcepath+"Rpdf\\生成代理签章.pdf"),
                        beanObj.getPdfBty());
            }
        }
    }
}
